package com.zoulongsheng.tworvdemo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class StudentExpandableAdapter extends BaseExpandableListAdapter {

    private Context ctx;
    private List<DataEntity> dataEntityList;
    private CheckBoxListener checkBoxListener;

    public StudentExpandableAdapter(Context context,List<DataEntity> dataEntityList){
        this.ctx = context;
        this.dataEntityList = dataEntityList;
    }

    /**
     * 获取租的数目
     * @return
     */
    @Override
    public int getGroupCount() {
        return dataEntityList == null ? 0 : dataEntityList.size();
    }

    /**
     * 获取指定组中子节点的数量
     * @param groupPosition
     * @return
     */
    @Override
    public int getChildrenCount(int groupPosition) {
        return dataEntityList.get(groupPosition).getChildrenDataList().size();
    }

    /**
     *获取与给定组关联的对象
     * @param groupPosition
     * @return
     */
    @Override
    public Object getGroup(int groupPosition) {
        return dataEntityList.get(groupPosition).getTitle();
    }

    /**
     * 获取与给定组中的给定子元素相关联的数据
     * @param groupPosition
     * @param childPosition
     * @return
     */
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return dataEntityList.get(groupPosition).getChildrenDataList().get(childPosition);
    }

    /**
     * 获取组在给定位置的id（唯一的）
     * @param groupPosition
     * @return
     */
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    /**
     * 获取给定组中给定子元素的ID
     * @param groupPosition
     * @param childPosition
     * @return
     */
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    /**
     * 确定ID，看是否总是指向同一个对象
     * @return
     */
    @Override
    public boolean hasStableIds() {
        return true;
    }

    /**
     * 返回组的对应视图，一级列表样式
     * @param groupPosition
     * @param isExpanded
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ParentHolder parentHolder;
        if(convertView == null){
            convertView = LayoutInflater.from(ctx).inflate(R.layout.parent_item,null);
            parentHolder = new ParentHolder();
            parentHolder.tvParent = convertView.findViewById(R.id.tv_parent);
            parentHolder.imgRight = convertView.findViewById(R.id.img_right);
            convertView.setTag(parentHolder);
        }else{
            parentHolder = (ParentHolder)convertView.getTag();
        }
        parentHolder.tvParent.setText(dataEntityList.get(groupPosition).getTitle());
        //共用一个右箭头，如果展开则顺时针90度旋转，否则不旋转
        if(isExpanded){
            parentHolder.imgRight.setRotation(90F);
        }else{
            parentHolder.imgRight.setRotation(0F);
        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildrenHolder childrenHolder;
        if(convertView == null){
            convertView = LayoutInflater.from(ctx).inflate(R.layout.son_item,null);
            childrenHolder = new ChildrenHolder();
            childrenHolder.tvChild = convertView.findViewById(R.id.tv_child);
            childrenHolder.checkBox = convertView.findViewById(R.id.checkbox);
            convertView.setTag(childrenHolder);
        }else{
            childrenHolder = (ChildrenHolder) convertView.getTag();
        }

        childrenHolder.checkBox.setChecked(dataEntityList.get(groupPosition).getChildrenDataList().get(childPosition).isSelect());
        childrenHolder.tvChild.setText(dataEntityList.get(groupPosition).getChildrenDataList().get(childPosition).getSubContent());
        childrenHolder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isChecked = !dataEntityList.get(groupPosition).getChildrenDataList().get(childPosition).isSelect();
                dataEntityList.get(groupPosition).getChildrenDataList().get(childPosition).setSelect(!isChecked);
                checkBoxListener.checkStateListener(groupPosition,childPosition,isChecked);
            }
        });
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    class ParentHolder{
        TextView tvParent;
        ImageView imgRight;
    }

    class ChildrenHolder{
        TextView tvChild;
        CheckBox checkBox;
    }

    /**
     * 用于提供对外复选框修改通知接口
     */
    public interface CheckBoxListener{
        void checkStateListener(int groupPosition,int childPosition,boolean isChecked);
    }

    public void setCheckBoxListener(CheckBoxListener checkBoxListener){
        this.checkBoxListener = checkBoxListener;
    }

    public void refreshData(List<DataEntity> dataEntityList){
        this.dataEntityList = dataEntityList;
        notifyDataSetChanged();
    }

}
